<?php

class Workset_Filter_TextToHtml implements Zend_Filter_Interface {

    protected $_options = array(
    	'tag' => 'p',
    	'class' => null
    );

	function __construct($options = null) {
    	if ($options !== null) {
			$this->setOptions($options);
		}
    }

    function setOptions($options) {
		if (count($options) == 1 && isInt(key($options))) {
			$options = array('tag' => current($options));
		}
		$this->_options = array_merge($this->_options, $options);
	}

	function filter($value) {

        $tag = $this->_options['tag'];

        $value = trim($value);
		$value = strip_tags($value, '<a><b><i><u><s><strike><strong><sub><sup><em>');

        $value = preg_replace("/\r?\n[\r\n]+/", '</' . $tag . '><' . $tag . '>', $value);
        $value = preg_replace("/\r?\n/", '<br/>', $value);

        $class = (isset($this->_options['class']) ? ' class="'. $this->_options['class'] .'"' : '');
        return '<' . $tag . $class . '>' . $value . '</' . $tag . '>';
	}

}